<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/12/13
 * Time: 18:12
 */
namespace common\logics\shop;

use common\widgets\Tool;
use yii\base\BaseObject;
use Yii;

class ShopLogic extends BaseObject implements IShop
{
    /** @var \common\sources\read\shop\ShopAccess $RShopAccess */
    private $RShopAccess;
    /** @var \common\sources\write\shop\ShopAccess $WShopAccess **/
    private $WShopAccess;
    /** @var \common\sources\read\sendddcode\SendDdCodeAccess $RSendDdCodeAccess **/
    private $RSendDdCodeAccess;
    /** @var \common\sources\write\sendddcode\SendDdCodeAccess $WSendDdCodeAccess **/
    private $WSendDdCodeAccess;
    /** @var \common\sources\read\businessadvice\BusinessAdviceAccess $RBusinessAdviceAccess  */
    private $RBusinessAdviceAccess;
	/** @var \common\sources\read\taskcommod\TaskCommodAccess $RTaskCommodAccess  */
    private $RTaskCommodAccess;

    public function init()
    {
        $this->RSendDdCodeAccess = Yii::$container->get("RSendDdCodeAccess");
        $this->WSendDdCodeAccess = Yii::$container->get("WSendDdCodeAccess");
        $this->WShopAccess = Yii::$container->get('WShopAccess');
        $this->RShopAccess = Yii::$container->get('RShopAccess');
        $this->RBusinessAdviceAccess = Yii::$container->get('RBusinessAdviceAccess');
        $this->RTaskCommodAccess = Yii::$container->get('RTaskCommodAccess');
        parent::init(); // TODO: Change the autogenerated stub
    }

    public function auditBranchList() {
        $list = $this->RShopAccess->auditBranchList();
        foreach ($list as &$value) {
            $value['create_time'] = empty($value['create_time']) ? '' : date("Y-m-d H:i:s",$value['create_time']);
        }
	    unset($value);
        return Tool::returnJson($list,200,"成功");
    }

    public function PrintExpress($data) {
        if(empty($data['shopId'])){
            return Tool::returnJson([],401,"操作店铺id不能为空");
        }
        if(empty($data['is_print'])){
            return Tool::returnJson([],401,"操作不能为空");
        }
        if(empty($data['sys_code'])){
            return Tool::returnJson([],401,"验证码不能为空");
        }
        if(empty($data['phone'])){
            return Tool::returnJson([],401,"操作人手机号不能为空");
        }
        //增加钉钉验证码=验证
        if(empty($data['sys_code'])){
            return Tool::returnJson([],401,"请填写验证码");
        }
        if(empty($data['phone'])){
            return Tool::returnJson([],401,"操作人手机号不能为空");
        }
        $smsArr = $this->RSendDdCodeAccess->getSendCode($data);
        if (intval($smsArr['code']) !== intval($data['sys_code'])) {
            return Tool::returnJson($data = [], $code = 405, '验证码输入有误');
        }
        //删除钉钉验证码
        $this->WSendDdCodeAccess->delDdCodeById($smsArr['id']);
        $result = $this->WShopAccess->PrintExpress($data);
        if ($result) {
            return Tool::returnJson([],200,'设置成功');
        }
        return Tool::returnJson([],409,'设置失败');
    }

    //审核店铺
    public function AuditBranch($data) {
        if(empty($data['shopId'])){
            return Tool::returnJson([],401,"操作店铺id不能为空");
        }
        if(empty($data['audit'])){
            return Tool::returnJson([],401,"操作不能为空");
        }
        if(empty($data['phone'])){
            return Tool::returnJson([],401,"操作人手机号不能为空");
        }
        $result = $this->WShopAccess->AuditBranch($data);
        if ($result) {
            return Tool::returnJson([],200,'设置成功');
        }
        return Tool::returnJson([],409,'设置失败');
    }

    public function AddDot($data) {
        if(empty($data['shopId'])){
            return Tool::returnJson([],401,"操作店铺id不能为空");
        }
        if(empty($data['dot'])){
            return Tool::returnJson([],401,"添加网点编号不能为空");
        }
	    if(empty($data['shop_name'])){
		    return Tool::returnJson([],401,"店铺名称 不能为空");
	    }
	    $shopName =   $this->RShopAccess->getShopOfName($data['shop_name'],$data['shopId']);
	    if($shopName){
		    return Tool::returnJson([],401,"店铺名已使用");
	    }
	    if(empty($data['ww_name'])){
		    return Tool::returnJson([],401,"旺旺名不能为空");
	    }
	    $shopOfWw = $this->RShopAccess->getShopOfWw($data['ww_name'], $data['shopId']);
	    if($shopOfWw){
		    return Tool::returnJson([],401,"旺旺名已使用");
	    }
	    if(empty($data['send_name'])){
		    return Tool::returnJson([],401,"发件姓名不能为空");
	    }
	    if(empty($data['send_phone'])){
		    return Tool::returnJson([],401,"发件人手机号不能为空");
	    }
	    if(empty($data['pro_name'])){
		    return Tool::returnJson([],401,"地区（省）不能为空");
	    }
	    if(empty($data['pro_code'])){
		    return Tool::returnJson([],401,"地区编码（省）不能为空");
	    }
	    if(empty($data['city_name'])){
		    return Tool::returnJson([],401,"地区（市）不能为空");
	    }
	    if(empty($data['city_code'])){
		    return Tool::returnJson([],401,"地区编码（市）不能为空");
	    }
	    if(empty($data['area_name'])){
		    return Tool::returnJson([],401,"地区（区）不能为空");
	    }
	    if(empty($data['area_code'])){
		    return Tool::returnJson([],401,"地区编码（区）不能为空");
	    }if(empty($data['addr'])){
		    return Tool::returnJson([],401,"详细地址不能为空");
	    }


	    $result = $this->WShopAccess->AddDot($data);
        if ($result) {
            return Tool::returnJson([],200,'添加成功');
        }
        return Tool::returnJson([],409,'添加失败');
    }

    //删除待审核店铺
    public function DelBranch($data) {
        if(empty($data['shopId'])){
            return Tool::returnJson([],401,"操作店铺id不能为空");
        }
        if(empty($data['sys_code'])){
            return Tool::returnJson([],401,"验证码不能为空");
        }
        if(empty($data['phone'])){
            return Tool::returnJson([],401,"操作人手机号不能为空");
        }
        //增加钉钉验证码=验证
        if(empty($data['sys_code'])){
            return Tool::returnJson([],401,"请填写验证码");
        }
        if(empty($data['phone'])){
            return Tool::returnJson([],401,"操作人手机号不能为空");
        }
        $smsArr = $this->RSendDdCodeAccess->getSendCode($data);
        if (intval($smsArr['code']) !== intval($data['sys_code'])) {
            return Tool::returnJson($data = [], $code = 405, '验证码输入有误');
        }
        //删除钉钉验证码
        $this->WSendDdCodeAccess->delDdCodeById($smsArr['id']);

        //检测 不可删除条件
	    $count  = $this->RTaskCommodAccess->getShopTaskCount(['shop_id' => $data['shopId']]);
		if($count != 0){
			return Tool::returnJson($data = [], $code = 401, '店铺不可删除');
		}
	    //删除数据
	    $result = $this->WShopAccess->delShop($data['shopId']);
	    if ($result) {
            return Tool::returnJson([],200,'删除成功');
        }
        return Tool::returnJson([],409,'删除失败');
    }

    //修改店铺名称
    public function ModifyBranch($data) {
        if(empty($data['shopId'])){
            return Tool::returnJson([],401,"操作店铺id不能为空");
        }
        if(empty($data['shop_name'])){
            return Tool::returnJson([],401,"店铺名称不能为空");
        }
        $shopName = $this->RShopAccess->getShopOfName($data['shop_name'],$data['shopId']);
        if ($shopName) {
            return Tool::returnJson([],401,"店铺名称已存在");
        }
        if(empty($data['ww_name'])){
            return Tool::returnJson([],401,"旺旺名称不能为空");
        }
        $wwName = $this->RShopAccess->getShopOfWw($data['ww_name'],$data['shopId']);
        if ($wwName) {
            return Tool::returnJson([],401,"旺旺名称已存在");
        }
        if(empty($data['sys_code'])){
            return Tool::returnJson([],401,"验证码不能为空");
        }
        if(empty($data['phone'])){
            return Tool::returnJson([],401,"操作人手机号不能为空");
        }
        //增加钉钉验证码=验证
        if(empty($data['sys_code'])){
            return Tool::returnJson([],401,"请填写验证码");
        }
        if(empty($data['phone'])){
            return Tool::returnJson([],401,"操作人手机号不能为空");
        }
        $smsArr = $this->RSendDdCodeAccess->getSendCode($data);
        if (intval($smsArr['code']) !== intval($data['sys_code'])) {
            return Tool::returnJson($data = [], $code = 405, '验证码输入有误');
        }
        //删除钉钉验证码
        $this->WSendDdCodeAccess->delDdCodeById($smsArr['id']);
        $result = $this->WShopAccess->ModifyBranch($data);
        if ($result) {
            return Tool::returnJson([],200,'修改成功');
        }
        return Tool::returnJson([],409,'修改失败');
    }

    public function ModifyHairInfo($data) {
        if(empty($data['shopId'])){
            return Tool::returnJson([],401,"操作店铺id不能为空");
        }
        if(empty($data['send_name'])){
            return Tool::returnJson([],402,"发件人姓名不能为空");
        }
        if(empty($data['pro_name'])){
            return Tool::returnJson([],403,"省名称不能为空");
        }
        if(empty($data['pro_code'])){
            return Tool::returnJson([],405,"省代号不能为空");
        }
        if(empty($data['city_name'])){
            return Tool::returnJson([],406,"市名称不能为空");
        }
        if(empty($data['city_code'])){
            return Tool::returnJson([],407,"市代号不能为空");
        }
        if(empty($data['area_name'])){
            return Tool::returnJson([],408,"区名称不能为空");
        }
        if(empty($data['area_code'])){
            return Tool::returnJson([],409,"区代号不能为空");
        }
        if(empty($data['addr'])){
            return Tool::returnJson([],510,"收货详细地址不能为空");
        }
        if(empty($data['send_phone'])){
            return Tool::returnJson([],511,"发件手机号不能为空");
        }
        if(empty($data['dot'])){
            return Tool::returnJson([],511,"网点编号不能为空");
        }
        $result = $this->WShopAccess->ModifyHairInfo($data);
        if ($result) {
            return Tool::returnJson([],200,'修改成功');
        }
        return Tool::returnJson([],409,'修改失败');
    }

    public function ShopAdvice($data) {
        $list = $this->RBusinessAdviceAccess->ShopAdvice($data);
        $count = $this->RBusinessAdviceAccess->ShopAdvice($data,true);
        return Tool::returnJson([
            'list' => $list,
            'page' => $data['page'],
            'count' => $count,
        ],200,'成功');
    }

    //删除商家建议
    public function DelShopAdvice($data) {
        if (empty($data['id'])) {
            return Tool::returnJson([],401,'建议id为空');
        }
        $result = $this->RBusinessAdviceAccess->DelShopAdvice($data);
        if ($result) {
            return Tool::returnJson([],200,'成功');
        }
        return Tool::returnJson([],409,'失败');
    }
}